﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;

namespace RpaDesigner.Frame
{
    public class ToolboxItem : ContentControl
    {
        private Point? dragStartPooint = null;

        static ToolboxItem()
        {
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
                typeof(ToolboxItem), new FrameworkPropertyMetadata(typeof(ToolboxItem)));
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            this.dragStartPooint = new Point?(e.GetPosition(this));
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.LeftButton != MouseButtonState.Pressed)
            {
                this.dragStartPooint = null;
            }

            if (!this.dragStartPooint.HasValue) return;

            var dataObject = new DragObject
            {
                Xaml = XamlWriter.Save(Content)
            };

            DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);

            e.Handled = true;
        }
    }

    public class DragObject
    {
        public string Key { get; set; }
        public string Xaml { get; set; }
        public Size? DesiredSize { get; set; }
    }
}